package com.android.pocketguru.Fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.pocketguru.Constants;
import com.android.pocketguru.Phrases;
import com.android.pocketguru.PopupActivity;
import com.android.pocketguru.R;


public class PopupFragment extends Fragment{

	private int phraseId = -1;
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
   	
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.popup, container, false);
    }
    
    @Override
    public void onStart() {
        super.onStart();

        
        Bundle args= getArguments();
        if (args !=null)
        	phraseId = args.getInt(Constants.PHRASE_ID_KEY);
        
        if (phraseId==-1)
        	phraseId = Phrases.GetRandomPhraseId(getActivity().getBaseContext());
        
        
        final Button btnShare = (Button) getActivity().findViewById(R.id.btnShare);
        btnShare.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	ShareFragment shareFragment = new ShareFragment();
            	
        		// Adding number to arguments object.
                Bundle args = new Bundle(); 
                args.putInt(Constants.PHRASE_ID_KEY, phraseId);
                
                shareFragment.setArguments(args);
            	
            	((PopupActivity) getActivity()).InflateFragment(shareFragment);
            }
        });
 
        LoadText();
        
        
        
        
        // Test button ======================================================================
        final Button btnSend = (Button) getActivity().findViewById(R.id.btn_Rand);
        btnSend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	phraseId = Phrases.GetRandomPhraseId(getActivity().getBaseContext());
            	LoadText();
            }
        });
    }
    
    
    private void LoadText()
    {
        TextView txt = (TextView) getActivity().findViewById(R.id.text_Wisdom);
        txt.setText(Phrases.GetPhraseById(getActivity().getBaseContext(), phraseId));	
    }
}
